//
//  CCompanyMgr.m
//  wanzhaoIM
//
//  Created by wanzhao on 15/9/10.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "CCompanyMgr.h"
#import "CCompany.h"
#import "FMDB.h"
#import "CFolderMgr.h"
#import "MicroDef.h"

@implementation CCompanyMgr

-(BOOL)insetCompany:(CCompany *)company
{
    if(company == nil)
    {
        return NO;
    } else {
        if (M_ISSTRNULL(company.compCode)) {
            return NO;
        }
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"INSERT INTO COMPANYINFO(compCode,callcenterTelephone,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?,?,?,?,?)",M_STOREVALUE(company.compCode),M_STOREVALUE(company.callcenterTelephone),M_STOREVALUE(company.yuLiuZiduan1),@(company.yuLiuZiduan2),M_STOREVALUE(company.yuLiuZiduan3)];
    }];
    return ret;

}

-(BOOL)batchInsertCompany:(NSArray *)compayList
{
    if(compayList == nil || [compayList count] <= 0)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inTransaction:^(FMDatabase *db, BOOL *rollback) {
        for(CCompany *company in compayList)
        {
            if (!M_ISSTRNULL(company.compCode)) {
                ret = [db executeUpdate:@"INSERT INTO COMPANYINFO(compCode,callcenterTelephone,yuLiuZiduan1,yuLiuZiduan2,yuLiuZiduan3) VALUES (?,?,?,?,?)",M_STOREVALUE(company.compCode),M_STOREVALUE(company.callcenterTelephone),M_STOREVALUE(company.yuLiuZiduan1),@(company.yuLiuZiduan2),M_STOREVALUE(company.yuLiuZiduan3)];
                if(!ret)
                {
                    *rollback = YES;
                }

            }
                   }
        
    }];
    return ret;

}

- (BOOL)updateCompany:(CCompany *)company
{
    if(company == nil)
    {
        return NO;
    }
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        ret = [db executeUpdate:@"UPDATE COMPANYINFO SET callcenterTelephone = ? where compCode = ?",M_STOREVALUE(company.compCode),M_STOREVALUE(company.callcenterTelephone)];
        
    }];
    return ret;

}

- (BOOL)loadAllCompany
{
    NSString *sql = nil;
    __block BOOL ret = NO;
    sql = [NSString stringWithFormat:@"select * from COMPANYINFO"];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        if(result == nil || ![result next]) {
            ret = NO;
        } else {
            ret = YES;
        }
        [result close];
    }];
    return ret;

}

- (BOOL)delAllCompany
{
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    __block BOOL ret = NO;
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        
        ret = [db executeUpdate:@"delete from COMPANYINFO"];
    }];
    return ret;

}

- (CCompany *)loadCompanyWithcompCode:(NSString *)compCode
{
    if(M_ISSTRNULL(compCode))
    {
        return nil;
    }
    NSString *sql = nil;
    __block CCompany *company = nil;
    sql = [NSString stringWithFormat:@"select * from COMPANYINFO where compCode = \'%@\'", compCode ];
//    FMDatabaseQueue *queen = [FMDatabaseQueue  databaseQueueWithPath:[[CFolderMgr shareInstance] personalDatabasePathRecently]];
    [[FMShareDataBase shareDataBase].shareQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *result = [db executeQuery:sql];
        while ([result next]) {
            company = [[CCompany alloc] init];
            company.companyIndex = [result  longLongIntForColumn:@"identifer"];
            company.callcenterTelephone = [result stringForColumn:@"callcenterTelephone"];
            company.compCode = [result stringForColumn:@"compCode"];
            company.yuLiuZiduan1 = [result stringForColumn:@"yuLiuZiduan1"];
        }
        [result close];
    }];
    return company;

}
@end
